package net.akkz.server.impl;

import java.util.ArrayList;

import net.akkz.dao.FultureDao;
import net.akkz.dao.FultureJoinDao;
import net.akkz.dbFactory.DbManage;
import net.akkz.dbFactory.Session;
import net.akkz.model.Fulture;
import net.akkz.model.FultureJoined;
import net.akkz.model.MyString;
import net.akkz.server.FultureServer;

public class FultureServerImpl implements FultureServer
{
	private FultureDao fultureDao;
	private FultureJoinDao fultureJoinDao;
	private DbManage dbManage;

	@Override
	public void addFulture(Fulture fulture)
	{
		Session session = dbManage.createSession();

		try
		{
			fultureDao.addFeature(session, fulture);
			
			ArrayList<Fulture> newFultures = fultureDao.findFultureByStudentId(session, fulture.getStudentid());
			Fulture newFulture = newFultures.get(newFultures.size() - 1);
			
			FultureJoined fultureJoined = new FultureJoined();
			fultureJoined.setFultureid(newFulture.getId());
			fultureJoined.setStudentid(newFulture.getStudentid());
			
			fultureJoinDao.addFultureJoined(session, fultureJoined);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public ArrayList<Fulture> findFulture(int start, int end)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Fulture> fultures = fultureDao.findFulture(session, start, end);

			session.commit();

			return fultures;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<Fulture> findFultureByStudentId(int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<Fulture> fultures = fultureDao.findFultureByStudentId(session, studentId);

			session.commit();

			return fultures;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<Fulture> findFultureOrderByJoined(int start, int end)
	{

		Session session = dbManage.createSession();

		try
		{
			ArrayList<Fulture> fultures = fultureDao.findFultureOrderByJoined(session, start, end);

			session.commit();

			return fultures;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public Fulture getFulture(int id)
	{
		Session session = dbManage.createSession();

		try
		{
			Fulture fulture = fultureDao.getFulture(session, id);

			session.commit();

			return fulture;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public void deleteFulture(int fultureId)
	{
		Session session = dbManage.createSession();

		try
		{
			fultureDao.deleteFulture(session, fultureId);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}

	@Override
	public void joinFulture(int fultureId, int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			Fulture fulture = fultureDao.getFulture(session, fultureId);
			fulture.setJoined(fulture.getJoined() + 1);

			FultureJoined fultureJoined = new FultureJoined();
			fultureJoined.setFultureid(fultureId);
			fultureJoined.setStudentid(studentId);

			fultureJoinDao.addFultureJoined(session, fultureJoined);
			fultureDao.updateFultureJoined(session, fulture);

			session.commit();
		}
		catch (Exception e)
		{
			session.rollback();
		}

	}

	@Override
	public void unJoinFulture(int fultureId, int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			FultureJoined fultureJoined = new FultureJoined();
			fultureJoined.setFultureid(fultureId);
			fultureJoined.setStudentid(studentId);
			
			if(fultureJoinDao.checkFultureJoined(session, fultureJoined))
			{
				Fulture fulture = fultureDao.getFulture(session, fultureId);
				fulture.setJoined(fulture.getJoined() - 1);
	
				fultureJoinDao.delFultureJoined(session, fultureJoined);
				fultureDao.updateFultureJoined(session, fulture);
	
				session.commit();
			}
		}
		catch (Exception e)
		{
			session.rollback();
		}
	}
	
	@Override
	public boolean checkJoinFulture(int fultureId, int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			FultureJoined fultureJoined = new FultureJoined();
			fultureJoined.setFultureid(fultureId);
			fultureJoined.setStudentid(studentId);
			
			boolean flag = fultureJoinDao.checkFultureJoined(session, fultureJoined);

			session.commit();
			
			return flag;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return false;
	}

	@Override
	public ArrayList<FultureJoined> findFultureJoinedByFultureId(int fultureId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<FultureJoined> fultureJoineds = fultureJoinDao.findFultureJoinedByFultureId(session, fultureId);

			session.commit();

			return fultureJoineds;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	@Override
	public ArrayList<FultureJoined> findFultureJoinedByStudentId(int studentId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<FultureJoined> fultureJoineds = fultureJoinDao.findFultureJoinedByStudentId(session, studentId);

			session.commit();

			return fultureJoineds;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}

	public void setFultureDao(FultureDao fultureDao)
	{
		this.fultureDao = fultureDao;
	}

	public void setDbManage(DbManage dbManage)
	{
		this.dbManage = dbManage;
	}

	public void setFultureJoinDao(FultureJoinDao fultureJoinDao)
	{
		this.fultureJoinDao = fultureJoinDao;
	}

	@Override
	public ArrayList<MyString> findFultureJoinedNameByFultureId(int fultureId)
	{
		Session session = dbManage.createSession();

		try
		{
			ArrayList<MyString> fultureJoineds = fultureJoinDao.findFultureJoinedNameByFultureId(session, fultureId);

			session.commit();

			return fultureJoineds;
		}
		catch (Exception e)
		{
			session.rollback();
		}

		return null;
	}
}
